package com.music.mysongs.helper

import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import android.widget.RemoteViews
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.core.content.ContextCompat.getSystemService
import com.music.mysongs.R
import com.music.mysongs.activity.SongActivity
import com.music.mysongs.utils.myLog

class NotifyHelper(context: Context) {
    private val cxt = context

    fun initNotification(title: String, artist: String, play: Boolean) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(
                "CHANNEL_ID",
                "CHANNEL_NAME",
                NotificationManager.IMPORTANCE_DEFAULT
            )
            val manager = getSystemService(
                cxt,
                NotificationManager::class.java
            )
            manager!!.createNotificationChannel(channel)
        }
        val myRemote = RemoteViews(cxt.packageName, R.layout.layout_service_notify)
        myRemote.setOnClickPendingIntent(
            R.id.btnLast_notify,
            PendingIntent.getBroadcast(
                cxt,
                0,
                Intent("com.music.mysongs.LAST"),
                PendingIntent.FLAG_UPDATE_CURRENT
            )
        )
        myRemote.setOnClickPendingIntent(
            R.id.btnPlayOrPause_notify,
            PendingIntent.getBroadcast(
                cxt,
                0,
                Intent("com.music.mysongs.PLAY"),
                PendingIntent.FLAG_UPDATE_CURRENT
            )
        )
        myRemote.setOnClickPendingIntent(
            R.id.btnNext_notify,
            PendingIntent.getBroadcast(
                cxt,
                0,
                Intent("com.music.mysongs.NEXT"),
                PendingIntent.FLAG_UPDATE_CURRENT
            )
        )
        myRemote.setTextViewText(R.id.txtTitle_notify, title)
        myRemote.setTextViewText(R.id.txtArtist_notify, artist)
        if (play) {
                   /* myRemote.setImageViewResource(
                        R.id.btnPlayOrPause_notify,
                        R.drawable.ic_song_play_128
                    ) *///不注释掉图片就bug
            myLog("notifyHelper play")
        } else {
                  /*myRemote.setImageViewResource(
                      R.id.btnPlayOrPause_notify,
                      R.drawable.ic_song_pause_128
                  )*/
            myLog("notifyHelper pause")
        }
        //发送通知
        val pendingIntent =
            PendingIntent.getActivity(cxt, 0, Intent(cxt, SongActivity::class.java), 0)
        val builder = NotificationCompat.Builder(cxt, "CHANNEL_ID")
        builder.setOngoing(true)
            .setAutoCancel(false)
            .setContent(myRemote)
            .setContentIntent(pendingIntent)
            .setSmallIcon(R.drawable.ic_space_logo_96)
        val managerCompat = NotificationManagerCompat.from(cxt)
        managerCompat.notify(1, builder.build())
    }

    fun stopNotify() {
        NotificationManagerCompat.from(cxt).cancel(1)
    }
}